# -*- coding: utf-8 -*-
"""
@Version : 
@Author  : ChenMZ
@Time    : 2025/09/24
@File    : CpuViewModel.py
@Desc    : 
@Modify  : 
"""
from typing import Any
from PyQt5.QtCore import Qt, QAbstractTableModel, QModelIndex


class _BaseTableViewModel(QAbstractTableModel):
    def __init__(self, headers: list):
        super().__init__()
        self._data: list = []
        self.header: list = headers

    def get_data(self, row: int):
        return self._data[row]

    def clear_data(self):
        self.beginResetModel()
        self._data = []
        self.endResetModel()

    def data(self, index, role=None):
        if role == Qt.DisplayRole:
            return self._data[index.row()][index.column()]
        if role == Qt.DecorationRole:
            pass

    def rowCount(self, parent: QModelIndex = ...) -> int:
        return len(self._data)

    def columnCount(self, parent: QModelIndex = ...) -> int:
        return len(self.header)

    def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> Any:
        if role != Qt.DisplayRole:
            return None
        if orientation == Qt.Horizontal:
            return self.header[section]
        return int(section + 1)


class TableViewModel(_BaseTableViewModel):
    def update_data(self, data: list):
        self.beginResetModel()
        self._data = data
        self.endResetModel()

    def update_header(self, header: list):
        self.beginResetModel()
        self.header = header
        self.endResetModel()
